package cn.edu.scau.cmi.huangzhiyuan.db.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.huangzhiyuan.db.domain.Student;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Teacher;

public class StudentDao extends HibernateBaseDao {
	
	public StudentDao() {
	}
	
	
	public void save(Student student) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.save(student);
			tx.commit();
		} catch (Exception e) {
			System.out.println("StudentDao save func has an error:" + e);
		}
	}
	
	
	public void delete(Student student) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.delete(student);
			tx.commit();
		} catch (Exception e) {
			System.out.println("StudentDao delete func has an error:" + e);
		}
	}
	
	public void update(Student student) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(student);
			tx.commit();
		} catch (Exception e) {
			System.out.println("StudentDao update func has an error:" + e);
		}
	}
	
	public Student findById(int id) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			Student student = session.get(Student.class, id);
			tx.commit();
			return student;
		} catch (Exception e) {
			System.out.println("StudentDao update func has an error:" + e);
			throw e;
		}
	}
	
	public List findAll() {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			String hql = "from Student";
			List students = session.createQuery(hql).list();
			tx.commit();
			return students;
		} catch (Exception e) {
			System.out.println("StudentDao findAll func has an error:" + e);
			throw e;
		}
	}
}
